/*!
 * \file t_multipart_data_end.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"

#include "../../../../src/module/http/config.h"
#include <http/comm/http_comm.h>
#include <http/http_core.h>
#include <frct_comm.h>
#include "frreport_comm.h"
#include "../../../comm/apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

static void __case1(void)
{
    char *p_tcp_body = NULL;
    unsigned int ui_tcp_body_len = 0;
    L7session_http_private *p_st_private = NULL;
    int i_ret = 0;


    i_ret = multipart_data_end(&p_tcp_body, &ui_tcp_body_len, p_st_private);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    http_session_destroy(p_st_private);
    return;
}

static void __case2(void)
{
    char *p_tcp_body = frstrdup((const char *)"--END\r\n");
    unsigned int ui_tcp_body_len = 7;
    L7session_http_private *p_st_private = NULL;
    Http1_session *p_sess = NULL;
    Http_comm_session *p_comm = NULL;
    int i_ret = 0;
    char *p_tmp = p_tcp_body;

    p_st_private = http_private_create();
    p_st_private->i_version = 1;
    p_st_private->p_http1_sess = http1_session_create();
    p_sess = p_st_private->p_http1_sess;
    p_comm = p_sess->p_st_comm_session;
    p_comm->p_boundary = frstrdup((const char *)"END");


    i_ret = multipart_data_end(&p_tcp_body, &ui_tcp_body_len, p_st_private);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_TODO);

    http_session_destroy(p_st_private);
    frfree(p_tmp);
    return;
}

static void __case3(void)
{
    char *p_tcp_body = frstrdup((const char *)"--END--\r\n");
    unsigned int ui_tcp_body_len = 9;
    L7session_http_private *p_st_private = NULL;
    Http1_session *p_sess = NULL;
    Http_comm_session *p_comm = NULL;
    int i_ret = 0;
    char *p_tmp = p_tcp_body;

    p_st_private = http_private_create();
    p_st_private->i_version = 1;
    p_st_private->p_http1_sess = http1_session_create();
    p_sess = p_st_private->p_http1_sess;
    p_comm = p_sess->p_st_comm_session;
    p_comm->p_boundary = frstrdup((const char *)"END");

    i_ret = multipart_data_end(&p_tcp_body, &ui_tcp_body_len, p_st_private);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);

    http_session_destroy(p_st_private);
    frfree(p_tmp);
    return;
}

static void __case4(void)
{
    char *p_tcp_body = frstrdup((const char *)"END");
    unsigned int ui_tcp_body_len = 3;
    L7session_http_private *p_st_private = NULL;
    Http1_session *p_sess = NULL;
    Http_comm_session *p_comm = NULL;
    int i_ret = 0;
    char *p_tmp = p_tcp_body;

    p_st_private = http_private_create();
    p_st_private->i_version = 1;
    p_st_private->p_http1_sess = http1_session_create();
    p_sess = p_st_private->p_http1_sess;
    p_comm = p_sess->p_st_comm_session;
    p_comm->p_boundary = frstrdup((const char *)"END");

    i_ret = multipart_data_end(&p_tcp_body, &ui_tcp_body_len, p_st_private);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    http_session_destroy(p_st_private);
    frfree(p_tmp);
    return;
}


int add_suite_multipart_data_end(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" multipart_data_end()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);

    return FR_SUCC;
}
